layui.use(['form', 'laypage'], function () {
    var $ = layui.jquery, form = layui.form, laypage = layui.laypage
    var count = $('input[name=\'totalElements\']').eq(0).val()//一共多少条数据
    var index = parent.layer.getFrameIndex(window.name);

    init();

    function init() {

        var gznr_id = $("input[name='gznr']").val();
        if ("" != gznr_id) {
            $(".choose").each(function (i,item) {
               if((","+gznr_id+",").indexOf(","+$(item).val()+",")!=-1){
                   $(item).prop("checked",true);
               }
            });
            form.render('checkbox');
        }
    }

    laypage.render({
        elem: 'pager' //注意，这里的 test1 是 ID，不用加 # 号
        , count: parseInt(count) //数据总数，从服务端得1到
        , curr: parseInt($('input[name=\'current\']').eq(0).val()),
        limit: parseInt($('input[name=\'rows\']').eq(0).val()),
        groups: 2, //连续显示分页数
        theme: '#1e9fff',
        skip: true,
        jump: function (obj, first) {//获取当前选中的页
            $('input[name=\'current\']').eq(0).val(obj.curr)
            $('input[name=\'rows\']').eq(0).val(obj.limit)
            if (!first) {
                $('#form').submit()
            }
        }
    })
    $(".custom-button").on("click", function () {
        var gznr = $('input[name=\'gznr\']').val();
        var flmc = $("input[name='fwdlmc']").val();
        if ("" == flmc) {
            flmc = "点击选择故障描述"
        }
        parent.document.getElementById("fwdlmc").innerText = flmc;
        parent.document.getElementById("gznr").value = gznr;
        if ("" == gznr) {
            parent.document.getElementById("fwdlmc").setAttribute("class", "layui-input gznr");
        } else {
            parent.document.getElementById("fwdlmc").setAttribute("class", "layui-input");
        }
        parent.layer.close(index);
    });
    form.on('checkbox(choose)', function (data) {
        var gzms = $('input[name=\'gznr\']').val()
        var fwdlmc = $('input[name=\'fwdlmc\']').val()
        var mc = $(data.othis).parents('td').next().html()
        if (data.elem.checked) {
            if ((',' + gzms + ',').indexOf(',' + data.value + ',') == -1) {
                if ('' == gzms) {
                    $('input[name=\'gznr\']').val(data.value)
                } else {
                    $('input[name=\'gznr\']').val(gzms + ',' + data.value)
                }
            }
            if ((';' + fwdlmc + ';').indexOf(';' + mc + ';') == -1) {
                if ('' == fwdlmc) {
                    $('input[name=\'fwdlmc\']').val(mc)
                } else {
                    $('input[name=\'fwdlmc\']').val(fwdlmc + ';' + mc)
                }
            }
        } else {
            if ((',' + gzms + ',').indexOf(',' + data.value + ',') != -1) {
                if ('' != gzms) {
                    var val = (',' + gzms + ',').replace(',' + data.value + ',', ',')
                    if ('' != val) {
                        if (',' == val) {
                            val = ''
                        } else {
                            val = val.substring(1, val.length - 1)
                        }
                    }
                    $('input[name=\'gznr\']').val(val)
                }
            }
            if ((';' + fwdlmc + ';').indexOf(';' + mc + ';') != -1) {
                if ('' != fwdlmc) {
                    var val = (';' + fwdlmc + ';').replace(';' + mc + ';', ';')
                    if ('' != val) {
                        if (';' == val) {
                            val = ''
                        } else {
                            val = val.substring(1, val.length - 1)
                        }
                    }
                    $('input[name=\'fwdlmc\']').val(val)
                }
            }
        }
    })
    $('input[name=\'fwsm\']').on('keyup', function () {
        var val = $(this).val()
        $.ajax({
            type: 'get',
            dataType: 'json',
            data: $('#form').serialize(),
            url: '/uc/apply/ajax/gznr',
            success: function (data) {
                var dataList = data.dataList
                var pager = data.pager
                var html = ''
                if ('' != dataList && dataList.length > 0) {
                    for (var i = 0; i < dataList.length; i++) {
                        var xml = '<tr>'
                        if (i % 2 == 0) {
                            xml = '<tr class=\'active\'>'
                        }
                        html = html + xml + '<td style=\'width: 40px\'>' +
                            '<input type=\'checkbox\' name=\'checked\' lay-skin=\'primary\' ' +
                            'value=\'' + dataList[i].id + '\' lay-filter=\'choose\' class=\'choose\'/>' +
                            '</td>' +
                            '<td>' + dataList[i].fwsm + '</td>'
                        '</tr>'
                    }
                }
                $('.custom-tr').html(html);
                form.render('checkbox');
                init();
                laypage.render({
                    elem: 'pager' //注意，这里的 test1 是 ID，不用加 # 号
                    , count: pager.totalElements //数据总数，从服务端得1到
                    , curr: 1,
                    limit: pager.rows,
                    groups: 2, //连续显示分页数
                    theme: '#1e9fff',
                    skip: true,
                    jump: function (obj, first) {//获取当前选中的页
                        $('input[name=\'current\']').eq(0).val(obj.curr)
                        $('input[name=\'rows\']').eq(0).val(obj.limit)
                        if (!first) {
                            $('#form').submit()
                        }
                    }
                })
            },
            error: function () {
                console.log('connect is error !')
            }
        })
    })
})
    